<?php

/**
 * This is the model class for table "graphs".
 *
 * The followings are the available columns in table 'graphs':
 * @property integer $id
 * @property string $name
 * @property string $filter
 * @property string $type
 * @property integer $datasourceId
 * @property string $xAxisLabel
 * @property string $yAxisLabel
 *
 * The followings are the available model relations:
 * @property Graphcolumn[] $graphcolumns
 * @property Graphtag[] $graphtags
 */
class GraphConfiguration extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GraphConfiguration the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'graphs';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, filter, type, datasourceId', 'required'),
			array('datasourceId', 'numerical', 'integerOnly'=>true),
			array('name, filter, type, xAxisLabel, yAxisLabel', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, filter, type, datasourceId, xAxisLabel, yAxisLabel', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'graphcolumns' => array(self::HAS_MANY, 'Graphcolumn', 'graphId'),
			'graphtags' => array(self::HAS_MANY, 'Graphtag', 'graphId'),
            'graph'=>array(self::BELONGS_TO,'graph','graphid'),
            'graphtag'=>array(self::BELONGS_TO,'graphtag','','on'=>'graphtag.graphId=graph.id'),//the foreign key is not primary

		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'filter' => 'Filter',
			'type' => 'Type',
			'datasourceId' => 'Datasource',
			'xAxisLabel' => 'X Axis Label',
			'yAxisLabel' => 'Y Axis Label',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('filter',$this->filter,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('datasourceId',$this->datasourceId);
		$criteria->compare('xAxisLabel',$this->xAxisLabel,true);
		$criteria->compare('yAxisLabel',$this->yAxisLabel,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function searchGraphs($tagName)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		/*$criteria=new CDbCriteria;
		$criteria->select='graphId,name,filter,type,datasourceId,xAxisLabel,yAxisLabel';
		$criteria->join= 'JOIN graphtag ON graphs.id = graphtag.graphId';
		//$criteria->with=array('graphtag','graphs');
		//$criteria->compare('graphtag.graphId',$this->id);
		$criteria->compare('graphtag.tagName',$tagName,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));*/
			$sql="SELECT graphId,tagName,name,filter,type,datasourceId,xAxisLabel,yAxisLabel FROM graphtag,graphs WHERE id=graphId and tagName like '$tagName'";
		$dataProviderGraph=new CSqlDataProvider($sql, array('sort'=>array(
        'attributes'=>array(
            'graphId','tagName','name'
        ),), 'id'=>'graphtag'
   
));

		$dataProviderGraph->getData(); 
		return $dataProviderGraph;
	}
}